<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<tiles:insertDefinition name="layout">
	<tiles:putAttribute name="head">
		<script type="text/javascript">
			$(function() {
				$("#datepicker").datepicker({
					showWeek : true,
					firstDay : 1,
					dateFormat : 'yy-mm-dd',
					showOtherMonths : true,
					selectOtherMonths : true,
					onSelect : function(date, source) {
						$("form").submit();
					}
				});

				$(".confirm").confirmDialog(function(data) {
					helper.showMsg(data, {
						okHandler : function() {
							location.reload();
						}
					});
				});

				$(".dialog").frameDialog({
					width : 800,
					height : 500,
					close : function() {
						if (art.dialog.data("success")) {
							location.reload();
						}
					}
				});

				$("#cbAll").click(function() {
					$("input[type=checkbox].remove").prop("checked", $(this).prop("checked"));
				});

				$("#btnRemove").click(function() {
					var cbs = $(".remove:checked");
					if (cbs.length == 0) {
						return false;
					}

					helper.confirm("确认", "你确定要删除选择的课表吗？<br/>因为在删除时要做较多检查，当选中项较多时可能要等待较长的时间。", function() {
						var ids = [];
						cbs.each(function() {
							ids.push($(this).val());
						});

						var url = "removeLesson.do";
						var params = "";
						for ( var i = 0; i < ids.length; i++) {
							params += ("id=" + ids[i] + "&");
						}

						var dialog = art.dialog({
							title : '删除确认',
							width : 500,
							height : 400,
							padding : 0,
							ok : true,
							close : function() {
								$("form.form-horizontal").submit();
							}
						});

						EasyUtils.setSilentAjax(true);
						$.post(url, params, function(content) {
							dialog.content(content).size(500, 400);
						});
					});

					return false;
				});

				$(".scroll").click(function() {
					var t = $(this).attr("href");
					$("html,body").animate({
						scrollTop : $(t).offset().top - 60
					}, "fast");
				});
			});
		</script>
	</tiles:putAttribute>
	<tiles:putAttribute name="mainBody">
		<form class="form-horizontal">
			<fieldset>
				<legend>每日课程表</legend>
				<div class="control-group">
					<div class="control-label">日期选择</div>
					<div class="controls">
						<a href="?startDate=${preDate}" class="btn">
							<i class="icon-chevron-left"></i>
						</a>
						<input type="text" name="startDate" id="datepicker" value="${currentDate}" />
						<a href="?startDate=${nextDate}" class="btn">
							<i class="icon-chevron-right"></i>
						</a>
						<a href="createLesson?startDate=${currentDate}" class="dialog btn btn-primary" title="创建新的课表" class="btn btn-primary">创建新课程</a>
						当日共 <strong> <s:property value="#lessons.size" />
						</strong> 节课
						<s:url id="print" namespace="/lesson" action="printDailyLesson">
							<s:param name="startDate" value="#currentDate"></s:param>
						</s:url>
						<a class="btn btn-primary" href="${print}" target="_blank">
							<i class="icon-print icon-white"></i>
						</a>
						<button class="btn" id="btnRemove">
							<i class="icon icon-remove"></i>删除选中课程
						</button>
					</div>
				</div>
			</fieldset>
		</form>
		<div class="alert alert-info">本日共 ${lessons.size()} 节课</div>
		<ul class="nav nav-tabs">
			<li class="active"><a href="#morning" class="scroll">Morning</a></li>
			<li><a href="#afternoon" class="scroll">Afternoon</a></li>
			<li><a href="#evening" class="scroll">Evening</a></li>
		</ul>
		<table class="table">
			<thead>
				<tr>
					<th><input type="checkbox" id="cbAll" /></th>
					<th>Time</th>
					<th>Lesson</th>
					<th>Teacher</th>
					<th>Classroom</th>
					<th>Type</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody id="selectable">
				<s:iterator value="lessons" var="lesson">
					<tr class="${lesson.courseType.cssClass}">
						<td><input type="checkbox" class="remove" value="${lesson.id}" /></td>
						<td><s:if test="isMorning(beginTime)">
								<a id="morning"></a>
							</s:if> <s:elseif test="isAfternoon(beginTime)">
								<a id="afternoon"></a>
							</s:elseif> <s:elseif test="isEvening(beginTime)">
								<a id="evening"></a>
							</s:elseif> ${lesson.timespan}</td>
						<td><s:url action="listListener" namespace="/lesson" id="listListener">
								<s:param name="id" value="id"></s:param>
							</s:url> <a href="${listListener}">${lesson.name}</a> <s:if test="canceled">
								<span class="label">已取消</span>
							</s:if></td>
						<td><s:iterator value="teachings" var="teaching" status="status">
								<s:url action="listTeacherLesson" namespace="/schedule" id="listTeacherLesson">
									<s:param name="teacherId" value="teacher.id"></s:param>
									<s:param name="startDate" value="#startDate"></s:param>
									<s:param name="endDate" value="#endDate"></s:param>
								</s:url>
								<a href="${listTeacherLesson}">${teaching.teacher.name}</a>
								<s:if test="!#status.last">,</s:if>
							</s:iterator></td>
						<td><s:url namespace="/schedule" action="listClassroomLesson" id="listClassroomLesson">
								<s:param name="classroomId" value="classroom.id"></s:param>
								<s:param name="startDate" value="#startDate"></s:param>
								<s:param name="endDate" value="#endDate"></s:param>
							</s:url> <a href="${listClassroomLesson}">${lesson.classroom.name}</a></td>
						<td><s:url action="listTypeLesson" namespace="/schedule" id="listTypeLesson">
								<s:param name="courseTypeId" value="courseType.id"></s:param>
								<s:param name="startDate" value="#startDate"></s:param>
								<s:param name="endDate" value="#endDate"></s:param>
							</s:url> <a href="${listTypeLesson }"> ${lesson.courseType.name}</a></td>
						<td><s:if test="canceled">
								<s:url namespace="/lesson" action="doResumeLesson" id="doResumeLesson">
									<s:param name="id" value="id"></s:param>
								</s:url>
								<a href="${doResumeLesson}" class="confirm" title="恢复该课程">恢复</a>
							</s:if> <s:else>
								<s:url namespace="/lesson" action="doCancelLesson" id="doCancelLesson">
									<s:param name="id" value="id"></s:param>
								</s:url>
								<a href="${doCancelLesson}" class="confirm" title="取消该课程">取消</a>
							</s:else> <s:url action="doRemoveLesson" namespace="/lesson" id="doRemoveLesson">
								<s:param name="id" value="id"></s:param>
							</s:url> <a href="${doRemoveLesson}" class="confirm" title="彻底删除该课程">删除</a> <s:url action="modifyLesson" namespace="/lesson" id="modifyLesson">
								<s:param name="id" value="id"></s:param>
							</s:url> <a href="${modifyLesson}" title="编辑" class="dialog">编辑</a></td>
					</tr>
				</s:iterator>
			</tbody>
			<tfoot></tfoot>
		</table>
	</tiles:putAttribute>
</tiles:insertDefinition>